package com.flyme.module.base.provider.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.DateUtils;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.api.SysUsercertificateRemoteService;
import com.flyme.module.base.client.dto.SysUserCertificateDto;
import com.flyme.module.base.client.dto.SysUserDto;
import com.flyme.module.base.client.entity.SysCertificate;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.client.entity.SysUsercertificate;
import com.flyme.module.base.provider.service.SysCertificateService;
import com.flyme.module.base.provider.service.SysUserService;
import com.flyme.module.base.provider.service.SysUsercertificateService;
import com.flyme.module.base.provider.service.SysUserexaminationService;
import com.flyme.module.file.client.entity.SysCourse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户证书管理(添加)
 * @author zyh
 * @date 2020-3-26
 */
@Api(tags = "用户证书")
@RestController
public class SysUsercertificateController extends BaseController<SysUsercertificateService, SysUsercertificate> implements SysUsercertificateRemoteService {

    @Autowired
    private SysUserService userService;
    @Autowired
    private SysCertificateService certificateService;
    @Autowired
    private SysUserexaminationService userexaminationService;


    /**
     * 用户证书列表
     * @return
     */
    @ApiOperation(value = "用户证书列表", notes = "用户证书列表")
    @Override
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysUserCertificateDto certificate) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("u.*,certificate.certificateName,user.userName,(SELECT ur.userName FROM sys_user ur WHERE ur.id = u.userId) userName2");
        cq.like("user.userName", certificate.getUserName());
        cq.like("u.idCard", certificate.getIdCard());
        cq.createAlias("certificate", "certificate");
        cq.createAlias("user", "user");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }

    /**
     * 删除用户证书
     * @return
     */
    @ApiOperation(value = "删除用户证书", notes = "删除用户证书")
    @Override
    public ResultBody delById(@PathVariable Long id) {
        bizService.removeById(id);
        return ResultBody.success("刪除成功");
    }

    /**
     * 更新用户证书
     */
    @ApiOperation(value = "更新用户证书", notes = "更新用户证书")
    @Override
    public ResultBody edit(@ModelAttribute SysUsercertificate certificate) {
        boolean tag = bizService.saveOrUpdate(certificate);
        return ResultBody.success(tag);
    }

    /**
     * 颁发证书
     */
    @ApiOperation(value = "颁发证书", notes = "颁发证书")
    @Override
    public ResultBody awardCertificate(@ModelAttribute SysUsercertificate usercertificate, Long userexaminationId) {
        Integer num = bizService.count(q().eq("userId", usercertificate.getUserId()).
            eq("certificateId", usercertificate.getCertificateId()));
        if(num > 0){
            return ResultBody.failed("该用户已登记过该证书，无法重复登记");
        }
        UpdateWrapper ue = new UpdateWrapper();
        boolean flag = userexaminationService.update(ue.set(true, "isAward", 1).set(true, "certificateId", usercertificate.getCertificateId())
            .eq(true, "id", userexaminationId));
        if(!flag){
            return ResultBody.failed("颁发证书失败");
        }
        SysUser user = userService.getById(usercertificate.getUserId());
        SysCertificate certificate = certificateService.getById(usercertificate.getCertificateId());
        usercertificate.setIdCard(user.getIdCard());
        usercertificate.setAwardTime(DateUtil.today());
        usercertificate.setSumperiod(0);
        if(certificate.getIsChange().equals(1) && certificate.getIsConvert().equals(0)){
            usercertificate.setChangeStatus(0);
        }
        boolean tag = bizService.saveOrUpdate(usercertificate);
        return ResultBody.success(tag);
    }

    /**
     * 用户证书列表
     * @return
     */
    @ApiOperation(value = "用户证书列表", notes = "用户证书列表")
    @Override
    public ResultBody userCertificateList(@ModelAttribute PagerInfo pagerInfo, Long userId) {
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.select("u.*,certificate.certificateName");
        cq.eq("u.userId", userId);
        cq.createAlias("certificate", "certificate");
        PqGrid pqGrid = bizService.pageList(cq);
        return ResultBody.success(pqGrid);
    }
}
